﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using ClassLibrary.Data;


public partial class UserControl_Booking : System.Web.UI.UserControl
{
    TblPhonesDAO PhonesDAO = new TblPhonesDAO(Global.CONNECTION_STRING);
    TblFaultListDAO FaultListDAO = new TblFaultListDAO(Global.CONNECTION_STRING);
    TblJobsDAO JobsDAO = new TblJobsDAO(Global.CONNECTION_STRING);
    TblCustomersDAO customerDAO = new TblCustomersDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnNext.Attributes.Add("onclick", "GetvalueListBox()");
            //ListFault.Attributes.Add("onChange", "GetvalueListBox()");
            LoadFaultList();
            

            if (Session[Global.SESSION_LOGGED_USER] != null)
            {
                int idCus = ((TblCustomers)Session[Global.SESSION_LOGGED_USER]).CustomerIndex;
                //LoadJobbyID(id);
                LoadCustomer(idCus);
            }
            else
            {
                txtDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            }
            
        }
    }

    private void LoadCustomer(int idCus)
    {
        DataTable dtCus = customerDAO.GetCustomerId(idCus);
        foreach (DataRow r in dtCus.Rows)
        {
            txtCustomer.Text = r["CustomerFullname"].ToString();
        }
    }

    private void LoadJobbyID(int JobNo)
    {
        DataTable dt_JobById = JobsDAO.GetJob(JobNo);

        foreach (DataRow r in dt_JobById.Rows)
        {
            txtJobNo.Text = r["JobID"].ToString();
            txtDate.Text = Convert.ToDateTime(r["DateBook"].ToString()).ToString(Global.DateFormat);

            
            string PaymentMethod =r["PaymentMethod"].ToString();
            if (PaymentMethod == "cash")
                rdCash.Checked = true;
            else
                rdOrther.Checked = true;

            drQuotationStatus.SelectedValue = r["QuotationStatus"].ToString();
            drApprovalStatus.SelectedValue = r["ApprovalStatus"].ToString();

            txtFaultDes.Text = r["FaultDescription"].ToString();

            LoadPhoneByid(Convert.ToInt32(r["PhoneID"]));


        }
    }

    private void LoadPhoneByid(int Phoneid)
    {
        DataTable dt_PhoneByid = PhonesDAO.GetPhoneById(Phoneid);
        foreach (DataRow r in dt_PhoneByid.Rows)
        {
            txtIMEI.Text = r["IMEINo"].ToString();
            txtModel.Text = r["Model"].ToString();
            txtManufacturer.Text = r["Manufacturer"].ToString();
            txtBrand.Text = r["PhoneBrand"].ToString();
            txtMSN.Text = r["PhoneMSN"].ToString();
            txtConditions.Text = r["PhoneCondition"].ToString();
            txtConditionDetails.Text = r["PhoneConditionDetails"].ToString();
            txtNetwork.Text = r["PhoneNetworkWith"].ToString();
        }
    }

    private void LoadFaultList()
    {
        ListFault.DataSource = FaultListDAO.GetFaultList();
        ListFault.DataTextField = "NameFault";
        ListFault.DataValueField = "FaultID";
        ListFault.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            #region Insert Phones

            TblPhones phone = new TblPhones();
            phone.IMEINo = txtIMEI.Text.Trim();
            phone.Model = txtModel.Text.Trim();
            phone.Manufacturer = txtManufacturer.Text.Trim();
            phone.PhoneBrand = txtBrand.Text.Trim();
            phone.PhoneMSN = txtMSN.Text.Trim();
            phone.PhoneCondition = txtConditions.Text.Trim();
            phone.PhoneConditionDetails = txtConditionDetails.Text.Trim();
            phone.PhoneNetworkWith = txtNetwork.Text.Trim();

            DataTable dt = null;

            if (txtJobNo.Text == "")
            {
                //phone.PhoneID = 0;
                dt = PhonesDAO.InsUpdPhone(phone, 1);
                foreach (DataRow dr in dt.Rows)
                    phone.PhoneID = Convert.ToInt32(dr["PhoneID"].ToString());
            }
            else
            {
                int JobNo = Convert.ToInt32(txtJobNo.Text);
                DataTable dt2 = JobsDAO.GetJob(JobNo);
                foreach(DataRow dr2 in dt2.Rows)
                {
                    phone.PhoneID = Convert.ToInt32(dr2["PhoneID"].ToString());
                }
                PhonesDAO.InsUpdPhone(phone, 2);
            }
            #endregion


            #region Jobs
            TblJobs Job = new TblJobs();
            Job.CustomerIndex = ((TblCustomers)Session[Global.SESSION_LOGGED_USER]).CustomerIndex;
            //Job.CustomerIndex = 1;
            Job.DateBook = DateTime.Now;
            Job.FaultDescription = txtFaultDes.Text;
            Job.PhoneID = phone.PhoneID;
            Job.CurrentStatus = 1;
            Job.ExpectedCompletionDate = DateTime.Now;
            Job.DateComplete = DateTime.Now;
            Job.RepairCharge = 0;
            Job.LPCharge = 0;
            Job.OrtherCharge = 0;
            Job.SalesAmount = 0;

            if (rdCash.Checked == true)
                Job.PaymentMethod = "Cash";
            else 
                Job.PaymentMethod = "Orther";

            Job.QuotationStatus = Convert.ToInt32( drQuotationStatus.SelectedValue);
            Job.ApprovalStatus = Convert.ToInt32(drApprovalStatus.SelectedValue);

            DataTable dtJobs = null;
            if (txtJobNo.Text == "")
            {
                Job.JobID = 0;
                dtJobs = JobsDAO.InsertUpdJob(Job, 1);
                foreach (DataRow drJob in dtJobs.Rows)
                {
                    Job.JobID = Convert.ToInt32(drJob["IDjob"].ToString());
                }
            }
            else
            {
                int Jobid = Convert.ToInt32(txtJobNo.Text);
                Job.JobID = Jobid;
                dtJobs = JobsDAO.GetJob(Jobid);

                JobsDAO.InsertUpdJob(Job, 2);
            }

            #endregion

            flag = true;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if(flag)
            Response.Redirect("~/Booking.aspx");
    }
    //protected void btnNext_Click(object sender, EventArgs e)
    //{
    //    for (int i = 0; i < ListFault.Items.Count - 1; i++)
    //    {
    //        if (ListFault.Items[i].Selected == true)
    //        {
    //            txtFaultDes.Text += ListFault.Items[i].Text + "<br/>";
    //        }
    //    }
    //}
    protected void btnPrint_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/ReportsJobSheet.aspx");
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        txtFaultDes.Text += ListFault.SelectedItem.Text + "; ";
        
    }
}
